package com.uniledger.test.network;

import android.text.TextUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkSingleton {

    private static final int DEFAULT_TIMEOUT = 15000;

    private OkHttpClient client;

    private String token;

    public void setToken(String token) {
        this.token = token;
    }

    public String getToken() {
        token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDM0NzUwOTgsImlkIjoiNyIsIm9yaWdfaWF0IjoxNTQwODgzMDk4fQ.GueZlAugd48g5g2XYuygYMIvAZ3Upb7fEUARnpyeQOc";
        if (TextUtils.isEmpty(token))
            return "Bearer ";
        return "Bearer " + token;
    }

    private OkSingleton() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .addInterceptor(new AuthorizationInterceptor())
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
                .build();
    }

    private static class SingletonInstance {

        private static final OkSingleton INSTANCE = new OkSingleton();

    }

    public static OkSingleton getInstance() {
        return SingletonInstance.INSTANCE;
    }

    public OkHttpClient getClient() {
        return client;
    }
}
